package nix.anim.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nix.anim.common.pojo.NixResult;
import nix.anim.mapper.RoleMapper;
import nix.anim.mapper.RoleRightMapper;
import nix.anim.pojo.Role;
import nix.anim.pojo.RoleExample;
import nix.anim.pojo.RoleRight;
import nix.anim.pojo.RoleRightExample;
import nix.anim.pojo.RoleRightExample.Criteria;
import nix.anim.service.RoleService;

/**
 * 角色管理Service
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RoleRightMapper roleRightMapper;
	
	@Value("${MAX_LIST_COUNT}")
	private Integer MAX_LIST_COUNT;
	
	@Override
	public NixResult deleteAll() {
		// 删除所有
		roleMapper.deleteByExample(new RoleExample());
		return NixResult.ok();
	}

	@Override
	public NixResult addRole(String name, String desc) {
		// 组装pojo
		Role role = new Role();
		role.setName(name);
		role.setRoleDesc(desc);
		// 添加
		NixResult result = addRole(role);
		return result;
	}

	@Override
	public NixResult getRoleList(Integer page, Integer rows) {
		if (page == null || page <= 0) {
			page = 1;
		}
		if (rows == null || rows <= 0 || rows > MAX_LIST_COUNT) {
			rows = MAX_LIST_COUNT;
		}
		// 分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		List<Role> list = roleMapper.selectByExample(new RoleExample());
		// 取查询结果
		PageInfo<Role> pageInfo = new PageInfo<>(list);
		// 返回
		return NixResult.ok(pageInfo);
	}

	@Override
	public NixResult getRoleById(Integer id) {
		if (id != null) {
			Role role = roleMapper.selectByPrimaryKey(id);
			if (role != null) {
				return NixResult.ok(role);
			} else {
				return NixResult.build(400, "id不存在！");
			}
		} else {
			return NixResult.build(400, "id无效！");
		}
	}

	@Override
	public NixResult addRole(Role role) {
		// 数据验证
		// 非空验证
		if (StringUtils.isBlank(role.getName())) {
			return NixResult.build(400, "角色名称不能为空！");
		}
		// 重复验证
		RoleExample roleExample = new RoleExample();
		nix.anim.pojo.RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andNameEqualTo(role.getName());
		List<Role> roleList = roleMapper.selectByExample(roleExample);
		if (roleList != null && roleList.size() > 0) {
			return NixResult.build(400, "角色名称已存在！");
		}
		// 补全属性
		role.setCreated(new Date());
		role.setUpdated(new Date());
		// 插入数据
		roleMapper.insert(role);
		return NixResult.ok(role);
	}

	@Override
	public NixResult updateRole(Role role) {
		// id验证
		NixResult result = getRoleById(role.getId());
		if (result.getStatus() != 200) {
			return result;
		}
		// 非空验证
		if (StringUtils.isBlank(role.getName())) {
			return NixResult.build(400, "角色名称不能为空！");
		}
		// 重复验证
		RoleExample roleExample = new RoleExample();
		nix.anim.pojo.RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andNameEqualTo(role.getName());
		criteria.andIdNotEqualTo(role.getId());
		List<Role> roleList = roleMapper.selectByExample(roleExample);
		if (roleList != null && roleList.size() > 0) {
			return NixResult.build(400, "角色名称已存在！");
		}
		// 更新属性
		role.setUpdated(new Date());
		// 更新
		roleMapper.updateByPrimaryKeySelective(role);
		return NixResult.ok();
	}

	@Override
	public NixResult getRoleRightIds(Integer id) {
		// id验证
		NixResult result = getRoleById(id);
		if (result.getStatus() != 200) {
			return result;
		}
		// 条件查询
		RoleRightExample example = new RoleRightExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(id);
		List<RoleRight> list = roleRightMapper.selectByExample(example);
		// 转为数组
		Integer[] ids = new Integer[list.size()];
		for (int i = 0; i < list.size(); i++) {
			ids[i] = list.get(i).getRightId();
		}
		return NixResult.ok(ids);
	}

	@Override
	public NixResult setRoleRight(Integer roleId, String rightIds) {
		// id验证
		NixResult result = getRoleById(roleId);
		if (result.getStatus() != 200) {
			return result;
		}
		// 删除现有权限
		RoleRightExample example = new RoleRightExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		roleRightMapper.deleteByExample(example);
		// 遍历插入新权限
		String[] ids = rightIds.split(",");
		for (String rightId : ids) {
			RoleRight roleRight = new RoleRight();
			roleRight.setRoleId(roleId);
			roleRight.setRightId(Integer.parseInt(rightId));
			roleRight.setCreated(new Date());
			roleRight.setUpdated(new Date());
			roleRightMapper.insert(roleRight);
		}
		return NixResult.ok();
	}

}
